package org.example.controllers;

import com.alibaba.fastjson.JSON;
import org.example.pojos.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Controller
public class TestController {

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/test")
    public Object test(HttpServletRequest request) throws IOException {
        Msg msg = new Msg();
        msg.setSourceIP(request.getRemoteAddr());

        Map<String,String> map = new HashMap<String, String>();

        Enumeration<String> headerNames = request.getHeaderNames();
        StringBuilder headers = new StringBuilder();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            map.put(headerName,headerValue);
        }
        msg.setHeaders(map);

        return msg;
    }

}
